package com.zb.service.imp.middleware;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zb.dao.ext.charger.OmsChargerPulseRecordDao;
import com.zb.dao.ext.charger.OmsChargerTelemetryRecordDao;
import com.zb.entity.base.JsonCommon;
import com.zb.entity.charger.OmsChargerPulseRecord;
import com.zb.entity.charger.OmsChargerTelemetryRecord;
import com.zb.service.middleware.HeartService;
import com.zb.util.RedisClient;
import com.zb.util.StringUtil;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HeartServiceImpl implements HeartService{
	@Resource
	OmsChargerPulseRecordDao omsChargerPulseRecordDao;
	@Resource
	OmsChargerTelemetryRecordDao omsChargerTelemetryRecordDao;
	float  ev3c_chargeQuantity  = 0,ev3c_chargeMoney=0;
	String responseMsg = "",protocol_type="",protocol_version="";
	String ev3c_frameCode;//ev3c服务器回复帧代码
	@Override
	public JsonCommon<String> uploadHeart(Map<String,String> map) {
		protocol_type = map.get("protocol_type");//协议类型  1：EV3C；2：Protocol Translate；3 P2P 
		protocol_version = map.get("protocol_version");
		String data = map.get("data");
		switch (protocol_type){
			case "1":
				ev3c_frameCode ="52";
				switch (protocol_version){
					case "0101":
						ev3cAnd0101(map,data);
						break;
					case "0102":
						//信息头
						ev3cAnd0102(map,data);
						//有信息体
						break;
					case "0103":
						//有信息头，无信息体
						ev3cAnd0103(map,data);
						break;
					default :
						break;
				}
				break;
			default :
				break;
		}
		//心跳的key由chargerId + 年月日时分 + heart 组成
		String key = map.get("chargerId") + "_"+ StringUtil.now12time() + "_heart";
		RedisClient.putMap(key, map);
		switch (protocol_type){
			case "1":
				responseMsg = ev3cResponseMsg(data,ev3c_frameCode);
				break;
			default :
				break;
		}
		StringUtil.sendSocket(responseMsg);
        return StringUtil.getjsonCReturn(responseMsg, "", "200", "ok"); 
	}
	
	public String ev3cResponseMsg(String data,String frameCode){
		 return data.substring(0, 34)+frameCode+data.substring(36,50);
	}
	
	public void ev3cAnd0101(Map<String,String> map,String data){
		//有信息体
		String codeIsBespeak = data.substring(50, 52); // 预约状态
		String codeIsCharge = data.substring(52, 54); // 充电桩状态
		ev3c_chargeQuantity= StringUtil.float16to10ByParamDesc(data.substring(54,62), 100); //codeThisKWH 充电电量 系数为0.01	放大100倍，低字节在前，高字节在后0x10  0x0e 0x00 0x00 表示充电电量为36KWH
		ev3c_chargeMoney= StringUtil.float16to10ByParamDesc(data.substring(62, 70), 100); //codeThisMoney 充电金额  类似电量
//		int codeThisParking = Integer.valueOf(data.substring(70, 72)); // 停车位状态
//		int codeThisParkingLock = Integer.valueOf(data.substring(72, 74)); // 停车位锁是否开启
		String codeError = data.substring(74, 76); //故障代码         
		map.put("bespeakStatus", codeIsBespeak);
		map.put("chargeStatus", codeIsCharge);
		map.put("chargeQuantity", String.valueOf(ev3c_chargeQuantity));
		map.put("chargeMoney",  String.valueOf(ev3c_chargeMoney));
		map.put("codeThisParking",data.substring(70, 72));
		map.put("codeThisParkingLock",data.substring(72, 74));
		map.put("codeError", codeError);
//		ev3c_frameCode ="";
	}
	
	public void ev3cAnd0102(Map<String,String> map,String data){
		ev3cAnd0101(map,data);
		//1.2协议中增加的充电信息  报文体
		float thisVoltage = StringUtil.float16to10ByParamDesc(data.substring(76,82), 100); //*充电电压(V)系数为0.01:低字节在前，高字节在后，数据放大100倍，如0x1D 0x57 0x00,表示实际数据为223.01V
		float thisElectricity = StringUtil.float16to10ByParamDesc(data.substring(82, 88), 100); //电流与电压类似
		int thisTime = (int)StringUtil.float16to10ByParamDesc(data.substring(88, 94), 100);//充电时间
//		int this_hour = thisTime/60;
//		int this_minute = thisTime%60;
		//*输出功率(W)	BIN码	3	系数为1	低字节在前，高字节在后，如 0x00 0xBC 0x1B,表示实际数据为 7100 W
		int this_power = (int)StringUtil.float16to10ByParamDesc(data.substring(94, 100), 1);//
		OmsChargerPulseRecord omsChargerPulseRecord = new OmsChargerPulseRecord();
		omsChargerPulseRecord.setCharge_amount(ev3c_chargeMoney);
		omsChargerPulseRecord.setCharge_quantity(ev3c_chargeQuantity);
		omsChargerPulseRecord.setCharger_id(map.get("chargerId"));
		omsChargerPulseRecord.setCreate_time(new Date());
		omsChargerPulseRecord.setPort(Integer.valueOf(map.get("source_port")));
		omsChargerPulseRecord.setValid_flag(1);
		omsChargerPulseRecordDao.save(omsChargerPulseRecord);
		OmsChargerTelemetryRecord omsChargerTelemetryRecord = new OmsChargerTelemetryRecord();
		omsChargerTelemetryRecord.setCharger_id(map.get("chargerId"));
		omsChargerTelemetryRecord.setCharger_power(this_power);
		omsChargerTelemetryRecord.setCreate_time(new Date());
		omsChargerTelemetryRecord.setElectricity(thisElectricity);
		omsChargerTelemetryRecord.setMinites(thisTime);
		omsChargerTelemetryRecord.setValid_flag(1);
		omsChargerTelemetryRecord.setVoltage(thisVoltage);
		omsChargerTelemetryRecordDao.save(omsChargerTelemetryRecord);
	}
	public void ev3cAnd0103(Map<String,String> map,String data){
		//do nothing
	}
}
